﻿#region summary
// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.
#endregion

using System.Collections.Generic;

namespace NSQL
{
    public class ElementCollection<T> : List<T>, IElementCollection<T>
    {
        /// <inheritdoc />
        public void Add(params T[] elements) => AddRange(elements);

        /// <inheritdoc />
        public void Add(IEnumerable<T> elements) => AddRange(elements);

        /// <inheritdoc />
        void IElementCollection<T>.Clear() => Clear();

        /// <inheritdoc />
        public void CopyTo(IElementCollection<T> to)
        {
            if (to == null) return;
            to.Clear();
            to.Add(this);
        }

        /// <inheritdoc />
        public void CopyFrom(IElementCollection<T> from)
        {
            if (from == null) return;
            Clear();
            AddRange(from);
        }

        /// <inheritdoc />
        void IElementCollection<T>.Remove(T element) => Remove(element);

        /// <inheritdoc />
        void IElementCollection<T>.Insert(int index, T element) => Insert(index, element);

        public ElementCollection(params T[] elements) => AddRange(elements);
        public ElementCollection(IEnumerable<T> elements) => AddRange(elements);

        protected void AddOne(T element) => base.Add(element);
    }

    public abstract class ElementCollection<T, TDefault> : ElementCollection<T> where TDefault : T
    {
        protected ElementCollection()
        {
        }
        protected ElementCollection(IEnumerable<T> elements): base(elements)
        {
        }
        public void Add(params TDefault[] elements) => AddRange(elements);

        public void Add(IEnumerable<TDefault> elements) => AddRange(elements);

        public void Remove(TDefault element) => base.Remove(element);

        public void Insert(int index, TDefault element) => base.Insert(index, element);

        protected void AddRange(IEnumerable<TDefault> elements)
        {
            foreach (var element in elements) AddOne(element);
        }
    }
}